package com.banzhi.shop.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banzhi.shop.R;

/**
 * Created by HT on 2017/6/26.
 * popupwindow模板
 */

public class TemplatePopupwindowUtils {
    /**
     * 提示开启权限的popupwindow
     *
     * @param activity
     * @param str1
     * @param str2
     */
    public static void showPermissionsPopupWindow(final Activity activity, String str1, String str2) {
        //设置contentView
        View contentView = LayoutInflater.from(activity).inflate(R.layout.popupwindow_get_personphone, null);
        final PopupWindow mPopWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        //设置各个控件的点击响应
        TextView tv1 = (TextView) contentView.findViewById(R.id.tv_know);
        TextView tv2 = (TextView) contentView.findViewById(R.id.tv_copy);
        TextView text1 = (TextView) contentView.findViewById(R.id.text1);
        TextView text2 = (TextView) contentView.findViewById(R.id.text2);
        if (ASUtils.isCanUse(str1)) {
            text1.setText(str1);
        }
        if (ASUtils.isCanUse(str2)) {
            text2.setText(str2);
        }
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("package:" + activity.getPackageName());//包名
                Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", uri);
                activity.startActivity(intent);
                mPopWindow.dismiss();
            }
        });

        mPopWindow.setFocusable(true);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1.0f, activity);
            }
        });
        setAlpha(0.5f, activity);
    }

    public static void setAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }


}
